/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2024/6/14
 * Time: 22:53
 */

#include "TrumpSelect.h"

namespace Upgrade {
    TrumpSelect::TrumpSelect(QWidget * _parent /* = nullptr*/)
        : QWidget(_parent)
    {
        setAttribute(Qt::WA_TranslucentBackground);//设置窗口背景透明
        setWindowFlags(Qt::FramelessWindowHint | Qt::WindowCloseButtonHint);

        InitWidget();
        InitUi();
        InitState();
    }

    void TrumpSelect::Check()
    {
        SetEnable(CardSuit::Unkonw, true);
        SetEnable(CardSuit::ClubsSuit, small_joker_count_ > 0 && suits_.testFlag(CardSuit::ClubsSuit));
        SetEnable(CardSuit::SpadesSuit, small_joker_count_ > 0 && suits_.testFlag(CardSuit::SpadesSuit));
        SetEnable(CardSuit::DiamondsSuit, big_joker_count_ > 0 && suits_.testFlag(CardSuit::DiamondsSuit));
        SetEnable(CardSuit::HeartsSuit, big_joker_count_ > 0 && suits_.testFlag(CardSuit::HeartsSuit));
        SetEnable(CardSuit::JokerSuit, big_joker_count_ > 1 || small_joker_count_ > 1);
    }

    void TrumpSelect::AddSuit(CardSuit _suit)
    {
        suits_ |= _suit;
        Check();
    }

    void TrumpSelect::AddJoker(CardRank _joker)
    {
        Q_ASSERT(_joker == CardRank::SmallJoker || _joker == CardRank::BigJoker);

        _joker == CardRank::SmallJoker ? small_joker_count_++ : big_joker_count_++;
        Check();
    }

    void TrumpSelect::SetEnable(CardSuit _suit, bool _enable)
    {
        suits_label_[_suit]->setEnabled(_enable);
    }

    void TrumpSelect::InitUi()
    {
        auto layout = new QHBoxLayout(this);

        layout->setContentsMargins(50, 4, 50, 4);
        layout->setSpacing(20);

        for(auto suit : suits_label_)
            layout->addWidget(suit);
    }

    void TrumpSelect::InitState()
    {
    }

    void TrumpSelect::InitWidget()
    {
        QList<QColor> colors = {Qt::black,Qt::red};

        auto mate = QMetaEnum::fromType<CardSuit>();
        for(int i = 0; i < mate.keyCount(); ++i) {
            auto suit  = static_cast<CardSuit>(mate.value(i));
            auto btn = new QPushButton(this);
            connect(btn,&QPushButton::clicked,this,[=]() {
                emit Select(suit);
            });
            btn->setFixedSize(80, 80);
            btn->setIconSize({80, 80});
            btn->setEnabled(false);
            btn->setStyleSheet(R"(
                background-color: transparent;
            )");

            suits_label_.insert(suit, btn);

            auto name   = GetSVGName(suit);
            auto data   = GetContent(name, colors[i % colors.size()]);
            auto pixmap = GetPixmap(data, {80, 80});

            btn->setIcon(pixmap);
        }
    }

    void TrumpSelect::paintEvent(QPaintEvent * event)
    {
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing, true);

        // 获取当前窗口大小
        QRect rect   = this->rect();
        int   width  = rect.width();
        int   height = rect.height();

        // 计算圆角半径（相对于窗口大小）
        int radius = height / 2;

        // 绘制圆角矩形背景
        QPainterPath path;
        path.addRoundedRect(rect, radius, radius);
        painter.fillPath(path, QColor(255, 255, 255));
        painter.setPen(Qt::black);
        painter.drawPath(path);

        QWidget::paintEvent(event);
    }

    void TrumpSelect::mousePressEvent(QMouseEvent * event)
    {
        // QWidget::mousePressEvent(event);
    }

    void TrumpSelect::mouseReleaseEvent(QMouseEvent * event)
    {
        // QWidget::mouseReleaseEvent(event);
    }

    void TrumpSelect::mouseMoveEvent(QMouseEvent * event)
    {
        // QWidget::mouseMoveEvent(event);
    }
} // Upgrade